package com.spontaneappnew.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.spontaneappnew.Categories;
import com.spontaneappnew.NavDrawerItem;
import com.spontaneappnew.NavMenuFBHeader;
import com.spontaneappnew.NavMenuItem;
import com.spontaneappnew.NavDrawerActivityConfiguration;
import com.spontaneappnew.Places;
import com.spontaneappnew.R;
import com.spontaneappnew.adapters.CategoriesAdapter;
import com.spontaneappnew.adapters.NavDrawerAdapter;

public class CategoriesActivity extends AbstractNavDrawerActivity {

	private CategoriesAdapter adapter = null;
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		adapter = new CategoriesAdapter(this,R.layout.category_list_row_layout, 
				Categories.getInstance().getCategories());
		listView = (ListView) findViewById(R.id.categories_listView);
		// Assign adapter to ListView
		listView.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.categories, menu);
		return true;
	}

	@Override
	protected void onNavItemSelected(int id) {
		Intent intent = null;
		switch ((int)id) {
        // Home nav button was pressed
        case 101:
            intent = new Intent(this, HomeActivity.class);
            break;
        // My places nav drawer was pressed
        case 102:
            intent = new Intent(this, MyPlacesNew.class);
            break;
        // Settings nav button was pressed
        case 103:
        	intent = new Intent(this, SettingsActivity.class);
            break;
	    }
		if(intent != null)
        	startActivity(intent);
		else
        	super.onNavItemSelected(id);
	}

	@Override
	protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {
		NavDrawerItem[] menu = new NavDrawerItem[] {
        		NavMenuFBHeader.create( 100, "User"),
                NavMenuItem.create(101, "Home", "ic_home", this),
                NavMenuItem.create(102, "My Places", "ic_menu_myplaces", this),
                NavMenuItem.create(103, "Settings", "ic_menu_settings", this),
                NavMenuItem.create(105, "About", "ic_action_about_light", this),
                NavMenuItem.create(107, "Rate Us!", "ic_whats_hot", this),
                NavMenuItem.create(108, "Logout", "ic_menu_logout", this)};
        
        NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
        navDrawerActivityConfiguration.setMainLayout(R.layout.activity_categories);
        navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
        navDrawerActivityConfiguration.setLeftDrawerId(R.id.left_drawer);
        navDrawerActivityConfiguration.setNavItems(menu);
        navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);       
        navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
        navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
        navDrawerActivityConfiguration.setBaseAdapter(
            new NavDrawerAdapter(this, R.layout.navdrawer_item, menu ));
        return navDrawerActivityConfiguration;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.action_choose_categories_done){
			if (Categories.getInstance().getSelectedCategories()==null){
				Toast.makeText(this, "You have not selected any categories", Toast.LENGTH_SHORT).show();
				finish();
				return true;
			}
			else {
				Places.getInstance().filterPlaces();
				Bundle bundle = getIntent().getExtras();
				Intent intent = new Intent(CategoriesActivity.this,FilteredPlacesActivity.class);
				if(bundle != null)
					intent.putExtras(bundle);
				startActivity(intent);
				return true;
			}
		} else 
			return super.onOptionsItemSelected(item);
	}

}
